package com.df.springmvc.system.auth.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;

import com.common.bean.SQLInsertBean;
import com.common.bean.SQLResultBean;
import com.df.springmvc.supper.SupperDao;

/**
 * 本类只做数据库操作处理
 * @author David
 */
public class AuthDao extends SupperDao{
	private String message;
	

	/**
	 * 获取模块
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<HashMap<String, Object>> getModularCode(String systemCode) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		String sql = "select distinct modularCode,modularName from s_auth_info where 1=1 and status=1 and systemCode= '"+systemCode+"' ";
		/*String sql = "select distinct modularCode,modularName from s_auth_info where 1=1 and status=1 ";*/
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{});
		ArrayList<HashMap<String, Object>> mapList = resultBean.getMapList();
		for(int i = 0 ; i < mapList.size() ; i++){
			list.add(mapList.get(i));
		}
		return list;
	}
	/**
	 * 获取模块功能
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<HashMap<String, Object>> getFunctionCode(String modularCode) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		String sql = "select distinct functionCode,functionName from s_auth_info where 1=1 and modularCode=?";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{modularCode});
		ArrayList<HashMap<String, Object>> mapList = resultBean.getMapList();
		for(int i = 0 ; i < mapList.size() ; i++){
			list.add(mapList.get(i));
		}
		return list;
	}
	/**
	 * 获取模块功能操作
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<HashMap<String, Object>> getOperationCode(String functionCode) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		String sql = "select authCode,operationCode,operationName from s_auth_info where 1=1 and functionCode=? order By operation_order";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{functionCode});
		ArrayList<HashMap<String, Object>> mapList = resultBean.getMapList();
		for(int i = 0 ; i < mapList.size() ; i++){
			list.add(mapList.get(i));
		}
		return list;
	}
	/**
	 * 获取角色
	 * @return
	 * @throws Exception
	 */
	public ArrayList<HashMap<String, Object>> getRole() throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		String sql = "select roleCode,roleName from s_role_info where 1=1 and status=1 ";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{});
		ArrayList<HashMap<String, Object>> mapList = resultBean.getMapList();
		for(int i = 0 ; i < mapList.size() ; i++){
			list.add(mapList.get(i));
		}
		return list;
	}
	/**
	 * 获取系统列表
	 * @return
	 * @throws Exception
	 */
	public ArrayList<HashMap<String, Object>> getGetSystem() throws Exception {
		// TODO Auto-generated method stub
		String sql="select systemCode,systemName from s_systemOpenStatus_info where systemStatus=1 ";
		return querySqlToSQLResultBean(sql).getMapList();
	}
	/**
	 * 配置角色权限
	 * @return
	 * @throws Exception
	 */
	public Object addRoleAuth(HashMap<String, String> map, ArrayList<String[]> list) throws Exception {
		// TODO Auto-generated method stub
		String result = "fail";
		int num=0;
		String roleCode = (String) map.get("roleCode");
		String systemCode = (String) map.get("systemCode");
		ArrayList<Object> listQuery = (ArrayList<Object>) Verify(roleCode,systemCode);
		int len = listQuery.size();
		if (len == 0) {
			result = "success";
		} else {
			Object o = delDate(roleCode,systemCode);
			if ((len + "").equals(o)) {
				result = "success";
			}
		}
		if (result == "success") {
			if(list.size()!=0){
				String[] arr = { "guid", "status", "createBy", "createTime","authCode","roleCode","systemCode"};
				for (String[] s : list) {
					HashMap<String, Object> hashMap = new HashMap<String, Object>();
					for (int i = 0; i < arr.length; i++) {
						
						hashMap.put(arr[i], s[i]);
					}
					
					if("".equals(hashMap.get("authCode"))){
						continue;
					}else if("".equals(hashMap.get("roleCode"))){
						continue;
					}else if("".equals(hashMap.get("systemCode"))){
						continue;
					}
					
					Object o = addDate(hashMap);
					if ("1".equals(o)) {
						num++;
					} else {
						break;
					}
				}
			}else{
				num=1;
			}
		}else{
			throw new RuntimeException("关联失败！");
		}
		return num;
	}
	/**
	 * 根据角色编号获取权限随机guid
	 * @param roleCode
	 * @param systemCode 
	 * @return
	 */
	public List Verify(String roleCode, String systemCode) {
		// TODO Auto-generated method stub
		Session session=getSessionFactory().getCurrentSession();
		String sql="select guid from sr_auth_role where roleCode=:roleCode and systemCode=:systemCode and STATUS = 1 ";
		SQLQuery query=session.createSQLQuery(sql);
		query.setString("roleCode",roleCode);
		query.setString("systemCode",systemCode);
		return query.list();
	}
	/**
	 * 删除相关角色权限
	 * @param roleCode
	 * @return
	 * @throws Exception
	 */
	public Object delDate(String roleCode,String systemCode) throws Exception {
		// TODO Auto-generated method stub
		String sql1 = "delete from sr_auth_role where roleCode = ? and systemCode=?";
		Object o = updateSql(sql1, new Object[] { roleCode,systemCode });
		return o;
	}
	/**
	 * 增加相关角色权限
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Object addDate(HashMap<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		SQLInsertBean insertBean=new SQLInsertBean("sr_auth_role");
		HashMap<String,Object> columnMap=new HashMap<String,Object>(map);
		insertBean.setColumnMap(columnMap);
		Object resulto=excuteSqlBean(insertBean);
		return resulto;
	}
	/**
	 * 根据角色编号获取权限
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public ArrayList<HashMap<String, Object>> getAuthCode(HashMap<String, String> map) throws Exception {
		// TODO Auto-generated method stub
		String sql="select authCode from sr_auth_role where status=1 and  roleCode=? and systemCode=?";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{map.get("roleCode"),map.get("systemCode")});
		return resultBean.getMapList();
	}
	
	/**
	 * 根据角色编号获取角色的所有权限信息（权限控制使用，公用方法）
	 */
	public ArrayList<HashMap<String, Object>> getAuthUseRoleCode(String roleCode) throws Exception {
		// TODO Auto-generated method stub
		//String sql="select a.authCode,a.systemCode,a.systemCode,a.modularCode,a.modularName,a.functionCode,a.functionName,a.operationCode,a.operationName from s_auth_info a INNER JOIN sr_auth_role ar on ar.authCode = a.authCode INNER JOIN s_role_info r on r.roleCode = ar.roleCode where ar.status ='1' and r.roleCode = ? ";
		String sql ="select a.authCode from s_auth_info a INNER JOIN sr_auth_role sar on sar.authCode = a.authCode INNER JOIN s_role_info r ON r.roleCode = sar.roleCode WHERE r.roleCode = ?";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{roleCode});
		return resultBean.getMapList();
	}
	
	/**
	 * 根据角色编号获取菜单权限（权限控制使用，公用方法）
	 */
	public ArrayList<HashMap<String, Object>> getMenuAuthUseRoleCode(String roleCode) throws Exception {
		// TODO Auto-generated method stub
		//String sql="select a.authCode,a.systemCode,a.systemCode,a.modularCode,a.modularName,a.functionCode,a.functionName,a.operationCode,a.operationName from s_auth_info a INNER JOIN sr_auth_role ar on ar.authCode = a.authCode INNER JOIN s_role_info r on r.roleCode = ar.roleCode where ar.status ='1' and r.roleCode = ? ";
		String sql ="select a.authCode from s_auth_info a INNER JOIN sr_auth_role sar on sar.authCode = a.authCode INNER JOIN s_role_info r ON r.roleCode = sar.roleCode WHERE r.roleCode = ?";
		SQLResultBean resultBean = querySqlToSQLResultBean(sql, new Object[]{roleCode});
		return resultBean.getMapList();
	}
	
}
